from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine(url='mysql+pymysql://root:123!#Qw@localhost/wangzhe?charset=utf8',
                       echo=True)
# 创建session，类似与游标cursor
session = sessionmaker(bind=engine)()
# 生成orm基类
Base = declarative_base()


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(32))
    pwd = Column(String(32))


class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    age = Column(Integer)
    sex = Column(String(50))
    country = Column(String(100))
    job = Column(Integer)


# 创建表结构
Base.metadata.create_all(engine)

obj = Person(name='孙权', age=33, sex='男', country='吴国', job=2)
session.add(obj)
# 提交数据
session.commit()
